home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 14164 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.4 KB  |  68 lines

  1. Path: ix.netcom.com!news
  2. From: identd@ix.netcom.com(none )
  3. Newsgroups: comp.lang.c
  4. Subject: Re: rand() source
  5. Date: 11 Apr 1996 23:37:59 GMT
  6. Organization: Netcom
  7. Message-ID: <4kk54n$m8k@dfw-ixnews2.ix.netcom.com>
  8. References: <4kf88n$j5e@news.ualr.edu>
  9. NNTP-Posting-Host: sea-wa11-16.ix.netcom.com
  10. X-NETCOM-Date: Thu Apr 11  6:37:59 PM CDT 1996
  11.  
  12. In <4kf88n$j5e@news.ualr.edu> luchini@hybrid.ualr.edu (Dr. Chris
  13. Luchini) writes: 
  14. >
  15. >does anyone have the source for the ran() or rand() functions?
  16. >I could really use it ASAP!
  17. >-c
  18. >
  19. >-- 
  20. > Chris Luchini, University of Arkansas at Little Rock, Hybrid Rocket
  21. Lab 
  22. >575 ETAS, 2801 S. University, Little Rock, AR 72204 (501) 569 8442
  23. fax-8020
  24.  
  25. I don't know if this is the source your thinking of...  but here is
  26. something off of the top of my head to make random numbers.  there is
  27. probably an easier way but I haven't been coding long: 
  28.  
  29. #include <stdio.h> 
  30. #include <stdlib.h> 
  31. #include <time.h> 
  32.  
  33. int rnd(int range);
  34. void seedrd(void);
  35.  
  36. main()
  37. {
  38.     int x; 
  39.     seedrnd(); 
  40.     
  41.     /*
  42.      * display a hundred random numbers
  43.      */ 
  44.  
  45.     for(x=0;x<100;x++)
  46.         printf("%i\n",rnd(10)); 
  47. }
  48.  
  49. int rnd(int range)
  50. {
  51.     int r; 
  52.  
  53.     r=rand()%range; 
  54.     return(r); 
  55. }
  56.  
  57. void seedrnd(void)
  58. {
  59.     srand((unsigned)time(NULL)); 
  60. }
  61.  
  62. /*
  63.  * i just realized as I typed this off of the top of my head that you 
  64.  * wanted the source to the actual rand() function.  oops.  may as well
  65.  * post it anyways. :-)  sorry. 
  66.  */
  67.  
  68.